package com.lianxueda.drawing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lianxueda.drawing.domain.dto.ClassQueryDTO;
import com.lianxueda.drawing.domain.pojo.Classes;

import com.lianxueda.drawing.domain.pojo.Courses;
import com.lianxueda.drawing.domain.pojo.Teachers;
import com.lianxueda.drawing.domain.vo.ClassVO;
import com.lianxueda.drawing.domain.vo.PageVO;

import com.lianxueda.drawing.mapper.ClassCoursesMapper;
import com.lianxueda.drawing.mapper.ClassMapper;
import com.lianxueda.drawing.mapper.CourseStudentMapper;
import com.lianxueda.drawing.mapper.TeacherMapper;
import com.lianxueda.drawing.service.ClassCoursesService;
import com.lianxueda.drawing.service.ClassService;
import com.lianxueda.drawing.service.CoursesService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Classes> implements ClassService {

    private final ClassMapper classMapper;
    private final TeacherMapper teacherMapper;
    private final ClassCoursesMapper  classCoursesMapper;
    private final CoursesService coursesService;



    @Override
    public PageVO<ClassVO> searchPage(ClassQueryDTO cqt) {
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();

        qw.like(StringUtils.hasText(cqt.getName()), Classes::getName, cqt.getName());

        if(StringUtils.hasText(cqt.getStartClassTime())){
            qw.apply("date_format(start_time,'%Y-%m-%d') >= {0}", cqt.getStartClassTime());
        }
        if(StringUtils.hasText(cqt.getEndClassTime())){
            qw.apply("date_format(start_time,'%Y-%m-%d') <= {0}", cqt.getEndClassTime());
        }
        IPage<Classes> page = new Page<>(cqt.getCurPage(), cqt.getPageSize());
        qw.orderByDesc(Classes::getId);
        qw.select(Classes.class, info ->!info.getColumn().equals("deleted"));

        IPage<Classes> pageResult = classMapper.selectPage(page, qw);


        List<Classes> classesList = pageResult.getRecords();


        List<ClassVO> classVOList = new ArrayList<>();
        for(int i = 0; i < classesList.size(); i++){
            Classes classes = classesList.get(i);


            ClassVO classVO = new ClassVO();
            BeanUtils.copyProperties(classes, classVO);


            List<Integer> coursesIdList = classCoursesMapper.getCoursesIdByClassId(classes.getId());


            if (coursesIdList == null || coursesIdList.isEmpty()) {

                classVO.setCourses(null);
            } else {

                List<Courses> coursesList = coursesService.selectCoursesByIds(coursesIdList);
                classVO.setCourses(coursesList);

                List<Integer> teacherIds = coursesList.stream()
                        .map(Courses::getTeacherId)
                        .filter(Objects::nonNull)  // 过滤掉 null 值
                        .distinct()  // 如果多个课程有相同的老师，去重
                        .collect(Collectors.toList());

                if (!teacherIds.isEmpty()) {
                    // 查询老师姓名
                    List<String> teacherNames = teacherMapper.getTeacherNamesByIds(teacherIds);
                    // 将老师姓名设置到 classVO 中
                    classVO.setTeacherName(teacherNames);
                }

            }

            classVOList.add(classVO);

        }



        PageVO<ClassVO> pageVO = new PageVO<>();
        pageVO.setCurrentPage(pageResult.getCurrent());
        pageVO.setPageSize(pageResult.getSize());
        pageVO.setTotal(pageResult.getTotal());
        pageVO.setList(classVOList);
        return pageVO;
    }

    @Override
    public Integer getClassIdByName(String className) {

        return classMapper.selectClassIdByName(className);
    }

    @Override
    public List<String> getClassNameList() {
        return classMapper.selectList(new LambdaQueryWrapper<Classes>()
                        .select(Classes::getName))  // 只选择 name 字段
                .stream()
                .map(Classes::getName)
                .collect(Collectors.toList());
    }



}
